package binary_tree.leetcode.medium;

/**
 * @author bruin_du
 * @description 从根节点到叶节点的路径数字之和
 * @date 2022/8/14 20:13
 **/
public class OfferII049_SumNumbers {
    public int sumNumbers(TreeNode root) {
        return recursion(root, 0);
    }

    private int recursion(TreeNode root, int num) {
        if (root == null) return 0;
        int next = num * 10 + root.val;
        if (root.left == null && root.right == null) return next;
        int left = recursion(root.left, next);
        int right = recursion(root.right, next);
        return left + right;
    }
}
